{% extends "index.html" %} 
<meta charset="UTF-8">

{% block index %}
{% csrf_token %}
<div class="x-nav">
	<span class="layui-breadcrumb">
        <a href="">资产管理</a>
      </span>
      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
      </a>
</div>
<div class="x-body">
	<div class="layui-row">
		<div class='layui-col-md6'>
	       <form class="layui-form layui-col-md12 x-so">
				<input class="layui-input" placeholder="资产名称" name="name" id="id_name">
				<input class="layui-input" placeholder="资产标识" name="key" id="id_key">
				<!--  
				<div class="layui-input-inline">
		           <select name="contrller" id ='id_area'>
		              <option value="">所属区域</option>
		              {% for item in area %}
		              	<option value="{{ item.id }}">{{ item.name }}</option>
		              {% endfor %}
		            </select>
		        </div>-->
		        <div class="layui-input-inline">
		            <select name="contrller" id ='id_asset_type'>
		              <option value="">资产类型</option>
		              {% for item in asset_type %}
		              	<option value="{{ item.id }}">{{ item.name }}</option>
		              {% endfor %}
		            </select>
		        </div>
	       </form>
	     </div>
	     <div class='UserTable'>
	       <button class="layui-btn"  data-type="reload"><i class="layui-icon" >&#xe615;</i></button>
	     </div>
     </div>

	<xblock>
		<div class="UserTable layui-row">
			<div class='layui-col-md10'>
				<button class="layui-btn " data-type="deletechoice"><i class="layui-icon">&#xe640;</i>删除资产</button>
				<button class="layui-btn" onclick="x_admin_show('资产申请','{% url 'assetrequest' %}',600)"><i class="layui-icon layui-icon-download-circle">	</i>资产申请</button>
			    <button class="layui-btn" onclick="x_admin_show('添加资产','{% url 'assetcreate' %}',600)"><i class="layui-icon"></i>新增资产</button>
			    <button class="layui-btn" onclick="x_admin_show('批量导入','{% url 'createuploadcsv' %}',600,400)"><i class="layui-icon">&#xe681;</i>批量导入</button>
			    <button class="layui-btn " data-type="portchoice"><i class="layui-icon">&#xe666;</i>端口扫描</button>
			    <button class="layui-btn " data-type="scanchoice"><i class="layui-icon">&#xe672;</i>安全扫描</button>
			    {% if user.is_superuser %}
				<button class="layui-btn " data-type="descoverchoice"><i class="layui-icon">&#xe628;</i>资产发现</button>
				<button class="layui-btn " data-type="assetuserchoice"><i class="layui-icon">&#xe601;</i>资产指定</button>
				{% endif %}
			</div>
			<div class='layui-col-md2' align='right'>
				<div><a href="{% url 'createoscsv' %}">os_csv模板</a>&<a href="{% url 'createwebcsv' %}">web_csv模板</a></div>
				<div><a href="{% url 'createvulncsv' %}">vuln_csv模板</a></div>
			</div>
		</div>
	</xblock>
	
	<table class="layui-hide" id="asset_table" lay-filter="asset"></table> 
</div>
<script>
var csrf = $('input[name="csrfmiddlewaretoken"]').val();

layui.use('table', function(){
  var table = layui.table;
  //方法级渲染
  table.render({
    elem: '#asset_table'
    ,url: "{% url 'assetlist' %}"
    ,method: 'post'
    ,where: {'csrfmiddlewaretoken':csrf}
    ,cols: [[
      {checkbox: true, fixed: true}
      ,{field:'asset_id', title: '资产编号', sort: true, fixed: true}
      ,{field:'asset_name', title: '资产名称'}
      ,{field:'asset_key', title: '资产标识', sort: true, event:'Asset'}
      ,{field:'asset_type', title: '资产分类', sort: true}
      ,{field:'asset_status', title: '资产状态',sort: true}
      ,{field:'asset_inuse', title: '认领状态'}
      //,{field:'asset_score', title: '风险估值',sort: true}
      ,{field:'user_email', title: '管理人员'}
      ,{field:'asset_updatetime', title: '更新时间',sort: true}
      ,{ title: '高级选项',fixed: 'right', align:'center', toolbar: '#barDemo'}
    ]]
    ,id: 'testReload'
    ,page: true
    ,limit: 15
    ,text:{
    	none:'暂无相关数据'
    }
  });
  
  table.on('tool(asset)', function(obj){
    var data = obj.data.asset_id;
    if(obj.event === 'Asset'){
    	url = "{% url 'assetdetails' '0' %}";
    	url = url.replace(/0/,data);
    	x_admin_show('资产详情',url);
    } else if(obj.event === 'edit'){
    	url = "{% url 'assetupdate' 'key' %}";
    	url = url.replace(/key/,data);
    	x_admin_show('资产详情',url,600);
    } else if(obj.event === 'detail'){
    	url = "{% url 'assetdetails' '0' %}";
    	url = url.replace(/0/,data);
    	window.open(url);
    }
  });
  
  
  
  var $ = layui.$, active = {
	deletechoice: function(){ //获取选中数据
    	//var csrf = $('input[name="csrfmiddlewaretoken"]').val();
	    var checkStatus = table.checkStatus('testReload')
	    ,data = checkStatus.data
	    ,list=new Array();
    	for(var i=0;i<data.length;i++){
    		list[i] = data[i].asset_id
    	}
    	layer.confirm('该操作为敏感操作<br>点击确认，将该选中资产从您名下移除',function(index){
		    $.post("{% url 'assetdelete' %}",{'asset_id_list':JSON.stringify(list),'action':'delete','csrfmiddlewaretoken': csrf},function(res){
	    		layer.msg(res.error);
	    		setTimeout(function(){
		    		location.replace(location.href);
		    	},2000)
	    	});
    	})
    }
	,portchoice: function(){ //获取选中数据
	    var checkStatus = table.checkStatus('testReload')
	    ,data = checkStatus.data
	    ,list=new Array();
	 	for(var i=0;i<data.length;i++){
	 		list[i] = data[i].asset_id
	 	}
	 	layer.confirm('该操作为敏感操作<br>点击确认，将会对所选资产进行端口扫描',function(index){
		    $.post("{% url 'assettaskaction' %}",{'asset_id_list':JSON.stringify(list),'action':'port','csrfmiddlewaretoken': csrf},function(res){
	    		layer.msg(res.error);
	    		setTimeout(function(){
		    		location.replace(location.href);
		    	},2000)
	    	});
	 	})
	 }
	,descoverchoice: function(){ //获取选中数据
	    var checkStatus = table.checkStatus('testReload')
	    ,data = checkStatus.data
	    ,list=new Array();
	 	for(var i=0;i<data.length;i++){
	 		if(data[i].asset_type === 'IP地址段'){
	 			list[i] = data[i].asset_id
	 		}
	 	}
	 	layer.confirm('该操作为敏感操作<br>点击确认，将会对所选网段资产进行资产发现，非网段资产将忽略',function(index){
		    $.post("{% url 'assettaskaction' %}",{'asset_id_list':JSON.stringify(list),'action':'segment','csrfmiddlewaretoken': csrf},function(res){
	    		layer.msg(res.error);
	    		setTimeout(function(){
		    		location.replace(location.href);
		    	},2000)
	    	});
	 	})
	 }
	,assetuserchoice: function(){ //获取选中数据
	    var checkStatus = table.checkStatus('testReload')
	    ,data = checkStatus.data
	    ,list=new Array();
	 	for(var i=0;i<data.length;i++){
	 		list[i] = data[i].asset_id
	 	}
	    $.post("{% url 'assetuseraction' %}",{'asset_id_list':JSON.stringify(list),'csrfmiddlewaretoken': csrf},function(res){
	    	url = "{% url 'assetuserdo' 'key' %}";
	    	url = url.replace(/key/,res.assetuser_id);
	    	x_admin_show('资产指定',url,600);
    	});
	 }
	,scanchoice: function(){ //获取选中数据
	    var checkStatus = table.checkStatus('testReload')
	    ,data = checkStatus.data
	    ,list=';';
	 	for(var i=0;i<data.length;i++){
	 		list=list + data[i].asset_id +';'
	 	}
    	url = "{% url 'taskscanchoice' 'key' %}";
    	url = url.replace(/key/,list);
    	x_admin_show('新建扫描',url,600);
	 }
  	,reload: function(){
      //执行重载
      table.reload('testReload', {
        page: {
          curr: 1 //重新从第 1 页开始
        }
        ,where: {
        	name: $('#id_name').val()
	    	,key: $("#id_key").val()
	    	,type: $("#id_asset_type option:selected").val()
	    	//,area: $("#id_area option:selected").val()
        }
      });
    }
  };
  $('.UserTable .layui-btn').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
});

</script>
<script type="text/html" id="barDemo">
<div class="layui-btn-group">
  	<a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon">&#xe642;</i>修改</a>
	<a class="layui-btn layui-btn-xs" lay-event="detail"><i class="layui-icon">&#xe60a;</i>详情</a>
</div>
</script>
{% endblock %}